﻿(function () {
    var siteName;
    $(function () {

        siteName = getSiteName();

        $("#basicInfo ul.key-value li")
        .each(function () {
            var $li = $(this);
            if ($li.attr("title") == "keyVal") {
                $li.append("<a href=\"javascript:;\" class=\"sync-link\">Add to sub sites</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:;\" class=\"sync-link-with-value\">Add to sub sites with value</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:;\" class=\"sync-link-remove\">Remove from sub sites</a>");
            }
        })
        .find("a.sync-link")
        .live("click", function () {
            saveCustomSettingToSubSite($(this), false);
        })
        .end()
        .find("a.sync-link-with-value")
        .live("click", function () {
            saveCustomSettingToSubSite($(this), true);
        })
        .end()
        .find("a.sync-link-remove")
        .live("click", function () {
            deleteCustomSettingFromSubSite($(this));
        })
        .end();
    });
    
    function getSiteName() {
        return urlHelper.getParamValue(document.location.href, "siteName");
    };

    function saveCustomSettingToSubSite($a, withValue) {
        var $inputs = $a.parent().find("input");
        var key = $($inputs[0]).val();
        var value = $($inputs[1]).val();

        $.post(
            "/Kooboo_CMS_CustomSettingSyncWrapper/CustomSetting/SyncAdd",
            {
                siteName: siteName,
                key: key,
                value: withValue ? value : ""
            },
            function (data) {
                if (data.Succ) {
                    alert("Add to sub sites successfully.");
                }
            }
        );
    };

    function deleteCustomSettingFromSubSite($a) {
        if (confirm("Are you sure to delete the custom setting from sub sites?") == false) {
            return;
        }

        var $inputs = $a.parent().find("input");
        var key = $($inputs[0]).val();

        $.post(
            "/Kooboo_CMS_CustomSettingSyncWrapper/CustomSetting/SyncDelete",
            {
                siteName: siteName,
                key: key,
            },
            function (data) {
                if (data.Succ) {
                    alert("Delete from sub sites successfully.");
                    $a.parent().remove();
                }
            }
        );
    };

    var urlHelper = {
        getParams: function (url) {
            var query = url.substring(url.indexOf("?") + 1);
            var items = query.split("&");

            var params = [];

            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                var _items = item.split("=");
                if (_items.length == 2) {
                    params.push({
                        key: _items[0].toLowerCase(),
                        value: _items[1]
                    });
                };
            };

            return params;
        },
        getParamValue: function (url, key) {
            var key = key.toLowerCase();
            var params = urlHelper.getParams(url);

            for (var i = 0; i < params.length; i++) {
                if (params[i].key == key) {
                    return params[i].value;
                };
            };

            return "";
        }
    };
})();